<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      Siege压力测试工具使用 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>Siege压力测试工具使用</h2>



  <p class="post-date">2020-02-10</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="Siege压力测试工具使用"><a href="#Siege压力测试工具使用" class="headerlink" title="Siege压力测试工具使用"></a>Siege压力测试工具使用</h1><h2 id="一-Siege介绍"><a href="#一-Siege介绍" class="headerlink" title="一 Siege介绍"></a>一 Siege介绍</h2><p>Siege是一款开源的压力测试工具，设计用于评估WEB应用在压力下的承受能力。可以根据配置对一个WEB站点进行多用户的并发访问，记录每个用户所有请求过程的相应时间，并在一定数量的并发访问下重复进行。siege可以从您选择的预置列表中请求随机的URL。所以siege可用于仿真用户请求负载</p>
<p>Siege官网：<a href="http://www.joedog.org/" target="_blank" rel="noopener">http://www.joedog.org/</a><br>Siege下载：wget <a href="http://www.joedog.org/pub/siege/siege-latest.tar.gz" target="_blank" rel="noopener">http://www.joedog.org/pub/siege/siege-latest.tar.gz</a></p>
<h2 id="二-windows-下使用"><a href="#二-windows-下使用" class="headerlink" title="二 windows 下使用"></a>二 windows 下使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#由于Siege不提供windows版本，但是有人通过Cygwin（Cygwin是一个在windows平台上运行的类UNIX模拟环境，让你的windows拥有Linux环境）基于Linux版的编译成windows版本：</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#地址为 https://github.com/ewwink/siege-windows</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#1 下载siege-windows-3.0.5.zip</span></span><br><span class="line"><span class="comment">#2 解压到 C:\siege-windows  （注意路径必须是这个，解压后文件夹名如果不是这个，要更改文件夹名，并放到c盘下）</span></span><br><span class="line"><span class="comment">#3 就可以愉快的使用了（当然你可以把该路径加入到环境变量中）</span></span><br></pre></td></tr></table></figure>

<h2 id="三-mac和linux使用"><a href="#三-mac和linux使用" class="headerlink" title="三 mac和linux使用"></a>三 mac和linux使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1 下载</span></span><br><span class="line">wget http://www.joedog.org/pub/siege/siege-latest.tar.gz</span><br><span class="line"><span class="comment">#2 解压</span></span><br><span class="line">tar -zxvf siege-latest.tar.gz</span><br><span class="line"><span class="comment">#3 切到目录下编译</span></span><br><span class="line">cd siege<span class="number">-3.1</span><span class="number">.4</span>/</span><br><span class="line">./configure</span><br><span class="line">make</span><br><span class="line">make install</span><br><span class="line"><span class="comment">#4 默认安装在/usr/local/bin/，并自动添加到系统环境变量中，在终端输入siege 如果可以显示出相应的命令介绍，则表示我们已经安装成功</span></span><br></pre></td></tr></table></figure>

<h2 id="四-使用"><a href="#四-使用" class="headerlink" title="四 使用"></a>四 使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 0、-c 100表示并发模拟100个用户并发，-r 10表示重复运行10次</span></span><br><span class="line">siege -c <span class="number">100</span> -r <span class="number">10</span> http://www.baidu.com</span><br><span class="line"><span class="comment"># 1、 启动100个用户（线程）在5分钟内对某一页面www.zihou.me进行并发访问</span></span><br><span class="line">siege http://www.baidu.com -c200 -t5</span><br><span class="line"><span class="comment"># 2、 对多个页面进行并发访问</span></span><br><span class="line">新建一个文件urls，里面的内容为（只是例子，任何url都可以）：</span><br><span class="line">http://www.zihou.me</span><br><span class="line">http://www.zihou.me/<span class="number">2010</span>/<span class="number">12</span>/<span class="number">16</span>/<span class="number">2821</span>/</span><br><span class="line">http://www.zihou.me/<span class="number">2010</span>/<span class="number">12</span>/<span class="number">14</span>/<span class="number">2806</span>/</span><br><span class="line">siege -f urls -c200 -t5</span><br><span class="line">表示启动<span class="number">200</span>个用户在<span class="number">5</span>分钟内并发访问以上的url网址。</span><br><span class="line"><span class="comment"># 3、 指定http请求头 文档类型</span></span><br><span class="line">siege -H <span class="string">"Content-Type:application/json"</span> -c <span class="number">200</span> -r <span class="number">100</span> -f urls.txt -i -b</span><br><span class="line"><span class="comment"># 4、 发送post请求，在网址后添加POST说明，并且紧跟参数在其后</span></span><br><span class="line">siege -c <span class="number">100</span> -r <span class="number">100</span> http://www.baidu.com/ POST k1=v1&amp;k2=v2</span><br></pre></td></tr></table></figure>

<h2 id="五-参数详解"><a href="#五-参数详解" class="headerlink" title="五 参数详解"></a>五 参数详解</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">-C,或–config 在屏幕上打印显示出当前的配置,配置是包括在他的配置文件$HOME/.siegerc中,可以编辑里面的参数,这样每次siege 都会按照它运行.</span><br><span class="line">-v 运行时能看到详细的运行信息</span><br><span class="line">-c n,或–concurrent=n 模拟有n个用户在同时访问,n不要设得太大,因为越大,siege 消耗本地机器的资源越多</span><br><span class="line">-i,–internet 随机访问urls.txt中的url列表项,以此模拟真实的访问情况(随机性),当urls.txt存在是有效</span><br><span class="line">-d n,–delay=n hit每个url之间的延迟,在<span class="number">0</span>-n之间</span><br><span class="line">-r n,–reps=n 重复运行测试n次,不能与 -t同时存在</span><br><span class="line">-t n,–time=n 持续运行siege ‘n’秒(如<span class="number">10</span>S),分钟(<span class="number">10</span>M),小时(<span class="number">10</span>H)</span><br><span class="line">-l 运行结束,将统计数据保存到日志文件中siege .log,一般位于/usr/local/var/siege .log中,也可在.siegerc中自定义</span><br><span class="line">-R SIEGERC,–rc=SIEGERC 指定用特定的siege 配置文件来运行,默认的为$HOME/.siegerc</span><br><span class="line">-f FILE, –file=FILE 指定用特定的urls文件运行siege ,默认为urls.txt,位于siege 安装目录下的etc/urls.txt</span><br><span class="line">-u URL,–url=URL 测试指定的一个URL,对它进行”siege “,此选项会忽略有关urls文件的设定</span><br></pre></td></tr></table></figure>

<h2 id="六-结果详解"><a href="#六-结果详解" class="headerlink" title="六 结果详解"></a>六 结果详解</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">Transactions: <span class="number">30000</span> hits //完成<span class="number">30000</span>次处理</span><br><span class="line">Availability: <span class="number">100.00</span> % //<span class="number">100.00</span> % 成功率</span><br><span class="line">Elapsed time: <span class="number">68.59</span> secs //总共使用时间</span><br><span class="line">Data transferred: <span class="number">817.76</span> MB //共数据传输 <span class="number">817.76</span> MB</span><br><span class="line">Response time: <span class="number">0.04</span> secs //响应时间，显示网络连接的速度</span><br><span class="line">Transaction rate: <span class="number">437.38</span> trans/sec //平均每秒完成 <span class="number">437.38</span> 次处理</span><br><span class="line">Throughput: <span class="number">11.92</span> MB/sec //平均每秒传送数据</span><br><span class="line">Concurrency: <span class="number">17.53</span> //实际最高并发连接数</span><br><span class="line">Successful transactions: <span class="number">30000</span> //成功处理次数</span><br><span class="line">Failed transactions: <span class="number">0</span> //失败处理次数</span><br><span class="line">Longest transaction: <span class="number">3.12</span> //每次传输所花最长时间</span><br><span class="line">Shortest transaction: <span class="number">0.00</span> //每次传输所花最短时间</span><br></pre></td></tr></table></figure>

<h2 id="七-修改系统的文件描述符限制"><a href="#七-修改系统的文件描述符限制" class="headerlink" title="七 修改系统的文件描述符限制"></a>七 修改系统的文件描述符限制</h2><p>当我们需要模拟大并发情况下的压测，则需要开启数量可观的线程，以及要占用大量的文件描述符，而系统默认对能够使用的文件描述符数量做了限制。首先使用 <code>ulimit -a</code> 命令查看目前系统开启的文件描述符数量</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">-t: cpu time (seconds)              unlimited</span><br><span class="line">-f: file size (blocks)              unlimited</span><br><span class="line">-d: data seg size (kbytes)          unlimited</span><br><span class="line">-s: stack size (kbytes)             <span class="number">8192</span></span><br><span class="line">-c: core file size (blocks)         <span class="number">0</span></span><br><span class="line">-v: address space (kbytes)          unlimited</span><br><span class="line">-l: locked-<span class="keyword">in</span>-memory size (kbytes)  unlimited</span><br><span class="line">-u: processes                       <span class="number">1418</span></span><br><span class="line">-n: file descriptors                <span class="number">256</span></span><br><span class="line">  </span><br><span class="line"><span class="comment"># 可以看到目前系统允许使用的文件描述符的数量为 256，这是远远不够的。如果不修改该参数，要是进行大量并发的模拟场景时则会报TOO MANY FILES OPEN 错误。为此我们可以先使用 ulimit -n 10000命令来调大系统可打开的文件描述符数量</span></span><br></pre></td></tr></table></figure>







</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#随笔" >
    <span class="tag-code">随笔</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/%E8%B7%AF%E9%A3%9E%E9%A1%B9%E7%9B%AE/6-%E8%AF%BE%E7%A8%8B%E6%A8%A1%E5%9D%97/3-%E8%AF%BE%E7%A8%8B%E4%B8%BB%E9%A1%B5%E4%B9%8B%E8%AF%BE%E7%A8%8B%E8%A1%A8%E6%95%B0%E6%8D%AE/">
        <span class="nav-arrow">← </span>
        
          python/路飞项目/6-课程模块/3-课程主页之课程表数据
        
      </a>
    
    
      <a class="nav-right" href="/article/1/01-%E6%8E%A5%E5%8F%A3%E7%9A%84%E5%B9%82%E7%AD%89%E6%80%A7/">
        
          接口的幂等性
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#Siege压力测试工具使用"><span class="toc-nav-text">Siege压力测试工具使用</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-Siege介绍"><span class="toc-nav-text">一 Siege介绍</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-windows-下使用"><span class="toc-nav-text">二 windows 下使用</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-mac和linux使用"><span class="toc-nav-text">三 mac和linux使用</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#四-使用"><span class="toc-nav-text">四 使用</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#五-参数详解"><span class="toc-nav-text">五 参数详解</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#六-结果详解"><span class="toc-nav-text">六 结果详解</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#七-修改系统的文件描述符限制"><span class="toc-nav-text">七 修改系统的文件描述符限制</span></a></li></ol></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/article/1/04-Siege压力测试工具使用/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "Siege压力测试工具使用",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>